package 代码记录.指定公司代码练习记录.面试手撕2024;


public class Test1 {
    /**
     * 力扣 - 80 - 删除排序数组中的重复项 II
     */
    public static void main(String[] args) {
        int[] arr = new int[]{1, 1, 1, 2, 2, 3};
        delete(arr);
    }

    public static void delete(int[] arr) {
        int left = 0, right = 0;
        int temp = 0;
        int resSize = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] == arr[i + 1]) {
                left = i;
            }
            while (arr[i] == arr[i + 1]) {
                i++;
            }
            right = i;
            int count = right - left + 1;
            while (count > 2) {
                swap(arr, temp, left);
                left++;
                temp++;
                resSize++;
                count--;
            }
        }
        System.out.println(arr.length - resSize);
        for (int i = resSize; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
